package cfca.seal.web.action.user;

import cfca.seal.bean.AdminUser;
import cfca.seal.bean.OperationLog;
import cfca.seal.bean.User;
import cfca.seal.common.Page;
import cfca.seal.exception.CodeException;
import cfca.seal.mananger.OperationLogManager;
import cfca.seal.util.StringUtil;
import cfca.seal.web.service.OperationLogService;
import cfca.seal.web.service.UserService;
import cfca.seal.web.system.SessionManager;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping({"/user"})
public class UserAction
{
  private String jumpListPagePath = "sys/user/list.html";
  private String jumpDetailPagePath = "sys/user/detail.html";
  private String jumpEditPagePath = "sys/user/edit.html";
  private String jumpAddPagePath = "sys/user/add.html";
  private String jumplistdo = "redirect:/user/v_list.do";
  private String applicationName = "用户管理";

  @Resource
  private UserService userService;

  @Resource
  private OperationLogService operationLogService;
  private static final Logger logger = LoggerFactory.getLogger("web_sys");

  @RequestMapping({"/v_list"})
  public String getAll(User user, String pageNo, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    try {
      AdminUser manager = SessionManager.getManager(request);
      Page page = this.userService.getUsers(user, pageNo, manager);
      modelMap.put("page", page);
      modelMap.put("user", user);
      return this.jumpListPagePath;
    } catch (Exception e) {
      logger.error("用户查询出现系统错误", e);
      throw new CodeException("C0001", e);
    }
  }

  @RequestMapping({"/v_edit"})
  public String viewEditPage(String editId, ModelMap modelMap) throws CodeException {
    try {
      User user = this.userService.getUserById(editId);
      modelMap.put("user", user);
      return this.jumpEditPagePath;
    } catch (Exception e) {
      logger.error("用户编辑出现系统错误", e);
      throw new CodeException("C0001", e);
    }
  }

  @RequestMapping({"/v_add"})
  public String viewAddPage(ModelMap modelMap) throws CodeException {
    User user = new User();
    user.setId(StringUtil.generateID());
    modelMap.put("user", user);
    return this.jumpAddPagePath;
  }

  @RequestMapping({"/v_detail"})
  public String view(String viewId, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    try {
      User user = this.userService.getUserById(viewId);
      List sealList = this.userService.getBindedSeals(viewId);
      modelMap.put("sealList", sealList);
      modelMap.put("user", user);
      return this.jumpDetailPagePath;
    } catch (Exception e) {
      logger.error("查看用户信息出现系统错误", e);
      throw new CodeException("C0001", e);
    }
  }

  @RequestMapping({"/o_add"})
  public String add(User user, ModelMap modelMap, HttpServletRequest request) throws CodeException {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "增加", user.getName(), "增加用户信息", null);
    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    try
    {
      boolean resultInfo = this.userService.addUser(user);
      modelMap.put("resultInfo", Boolean.valueOf(resultInfo));
      return this.jumplistdo;
    } catch (CodeException ce) {
      operationLog.setResult(10);
      logger.error("添加用户信息出现错误", ce);
      throw new CodeException(ce.getCode(), ce.getMessage());
    } catch (Exception e) {
      operationLog.setResult(10);
      logger.error("添加用户信息出现系统错误", e);
      throw new CodeException("C0001", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }

  @RequestMapping({"/o_edit"})
  public String edit(User user, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "修改", user.getName(), "修改用户信息", null);
    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    try
    {
      boolean resultInfo = this.userService.update(user);
      modelMap.put("resultInfo", Boolean.valueOf(resultInfo));
      return this.jumplistdo;
    } catch (CodeException ce) {
      operationLog.setResult(10);
      logger.error("编辑用户信息出现错误", ce);
      throw new CodeException(ce.getCode(), ce.getMessage());
    } catch (Exception e) {
      operationLog.setResult(10);
      logger.error("编辑用户信息出现系统错误", e);
      throw new CodeException("C0001", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }

  @RequestMapping({"/o_delete"})
  public String delete(String deleteId, String name, ModelMap modelMap, HttpServletRequest request) throws CodeException
  {
    OperationLog operationLog = OperationLogManager.getInstance(request, this.applicationName, "删除", name, "删除用户信息", null);
    operationLog.setOrganizationId(SessionManager.getManager(request).getOrganizationId());
    try
    {
      List sealList = this.userService.getBindedSeals(deleteId);
      if ((null != sealList) && (sealList.size() > 0)) {
        operationLog.setResult(10);
        throw new CodeException("C4000", "该用户已绑定印章，无法删除");
      }

      boolean result = this.userService.deleteById(deleteId);
      modelMap.put("resultInfo", Boolean.valueOf(result));
      return this.jumplistdo;
    } catch (CodeException ce) {
      operationLog.setResult(10);
      logger.error("删除用户信息出现错误", ce);
      throw new CodeException(ce.getCode(), ce.getMessage());
    } catch (Exception e) {
      operationLog.setResult(10);
      logger.error("删除用户信息出现系统错误", e);
      throw new CodeException("C0001", e);
    } finally {
      this.operationLogService.addOperationLog(operationLog);
    }
  }
}